<template>
    <div class="container">
        <h3>打印管理</h3>
        <el-row style="margin:15px 0 20px 0">
            <el-col :span="7">
                <el-input placeholder="请输入关键词" style="width:300px" v-model.trim="searchName">
                    <i slot="prefix" class="el-input__icon el-icon-search"></i>
                    <el-button slot="append" @click="handleSearch">搜索</el-button>
                </el-input>
            </el-col>
          <el-button type="primary" style="float:right;  margin-right: 10px" @click="download">导出</el-button>
        </el-row>
        <el-row>
            <el-table
                :data="tableData"
                border
                ref="multipleTable"
                header-cell-class-name="table-header"
            >
				<el-table-column v-if='switchHeadTab=="生产系统"' prop="inventoryType" sortable label="产品大类"></el-table-column>
                <el-table-column prop="breedName" sortable label="名称"></el-table-column>
				<el-table-column prop="batch" sortable label="包装批号"></el-table-column>
				<el-table-column prop="time" sortable label="生产日期"></el-table-column>
				 <el-table-column prop="deadLine" sortable label="保质期（月）"></el-table-column>
                <el-table-column prop="dispenserCode" sortable label="发药机编码"></el-table-column>
                <el-table-column prop="type" sortable label="状态">
                    <template slot-scope="scope">
                        {{scope.row.stauts == 1?'已发送':'未发送'}}<span v-if="scope.row.stauts == 1">({{scope.row.lastPrintNum}})</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="150" align="center">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            @click="handleView(scope.$index, scope.row)"
                        >查看</el-button>
                        <span v-if="scope.row.isRecall == 0">
                        <el-button
                            type="text"
                            v-if="qx"
                            @click="handlePrint(scope.$index, scope.row)"
                        >发送</el-button>
                           <el-button
                               type="text"
                               v-if="fs"
                               @click="handleadd(scope.$index, scope.row)"
                           >增加打印量</el-button>
                        </span>
                        <el-button
                            v-if="scope.row.isRecall == 1"
                            type="info"
                            disabled
                        >已召回</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination layout="total, prev, pager, next, jumper" :page-size="pagesize1" @current-change="handleCurrentChange1" :total="total1" :current-page.sync="currentPage1" style="margin-top:20px"></el-pagination>
        </el-row>
      <el-dialog title="实际产量" :visible.sync="dialogFormVisible">
        <el-form :model="c" :rules="rules" ref="ruleForm">
          <el-row>
            <el-col :span="16">

              <el-form-item label="增加打印量：" prop="number" label-width="120px">
                <el-input v-model.trim="c.number" style="width:300px">


                </el-input>

              </el-form-item>

            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
        </div>
      </el-dialog>
    </div>
</template>

<script>
import axios from "axios"
import {print_findByList,addPrintNumber} from "@/request/api";
import { mapState } from 'vuex';
    export default {
        data() {
            return {
              rules:{},
              c:{},
              number:'',
              dialogFormVisible:false,
                searchName: '',
                tableData: [],
                total1: 0,
                currentPage1:1,
                pagesize1:10,
                qx:true,
               fs:true,
              index:''
            };
        },
        created(){
            var data = JSON.stringify(this.$store.state.dy)
            var a = "发送"
          var b="增加打印量"
            if(data.indexOf(a) != -1){
                this.qx = true
            }else{
                this.qx = false
            }

          if(data.indexOf(b) != -1){
            this.fs = true
          }else{
            this.fs = false
          }

        },
        mounted(){
            this.init()
        },
        computed:{
            ...mapState(['switchHeadTab'])
        },
        methods: {
          handleadd(index,row){
            this.dialogFormVisible = true
            this.index = index
          },
          submitForm(){
            if(this.number = ""){
              this.$notify({
                title: '提示',
                message: '增加打印量不能为空',
                type: 'info',
                duration:1000
              });
            }else{
              var data = this.tableData[this.index]
              data.printNum = Number(data.printNum) + Number(this.c.number)
              addPrintNumber(data).then((res)=>{
                this.dialogFormVisible = false
                this.init()
              })
            }
          },
          download(){
            //  window.location.href = "http://" + window.location.host + "/user/outUser";
            // window.location.href = "http://192.168.0.144:81/pack/exportBatch";
            axios({
              method: 'GET',
              url: '/pack/exportBatch',
              responseType: 'blob'
            }).then(res=>{
              // console.log(res)
              this.downloadFile(res, '数据.xls')
            }).catch(err=>{
              // console.log(err)
            })
          },

          downloadFile(res, fileName) {
            let blob = new Blob([res.data]);
            if (!fileName) {
              fileName = res.headers['content-disposition'].split('filename=').pop();
            }
            if ('msSaveOrOpenBlob' in navigator) {
              window.navigator.msSaveOrOpenBlob(blob, fileName);
            } else {
              const elink = document.createElement('a');
              elink.download = fileName;
              elink.style.display = 'none';
              elink.href = URL.createObjectURL(blob);
              document.body.appendChild(elink);
              elink.click();
              URL.revokeObjectURL(elink.href);
              document.body.removeChild(elink);
            }
          },
            // 调用接口
            init(){
                print_findByList({
                    type: 1,
                    searchName: this.searchName,
                    page: this.currentPage1,
                    materialType:this.switchHeadTab == '中药饮片' ? this.switchHeadTab:'配方颗粒'
                }).then((res) =>{
                    this.tableData = res.rows
                    this.total1 = res.total
                })
            },
            // 查看
            handleView(index,row){
                var endNum = Math.ceil((row.lastPrintNum/1.1)-row.printNum)
                this.$router.push({
                    path: '/printTask/viewPrint',
                    query: { id: row.id, lastPrintNum: row.lastPrintNum, endNum: endNum }
                });
            },
            // 发送
            handlePrint(index,row){
              console.log(row)
                var endNum = Math.ceil(row.printNum-(row.lastPrintNum/1.1))
                if(this.switchHeadTab == '生产系统'){
                    this.$router.push({
                        path: '/printTask/editPrint',
                        query: { id: row.id, lastPrintNum: row.lastPrintNum, endNum: row.printNum ,bench:row.batch }
                    });
                }else{
                    this.$router.push({
                        path: '/printTask/medicalHerbPrintTemplate',
                        query: { id: row.id, lastPrintNum: row.lastPrintNum, endNum: row.printNum ,bench:row.batch }
                    });
                }
            },
            // 搜索
            handleSearch() {
                this.currentPage1 = 1
                this.init()
            },
            handleCurrentChange1(val){
                this.currentPage1 = val
                this.init()
            },

        }
    }
</script>

<style scoped>
.el-pagination{
    text-align: center;
}
.el-col{
    margin-bottom: 0;
}
.el-select .el-input {
width: 130px;
}
.input-with-select .el-input-group__prepend {
background-color: #fff;
}
.el-input--small .el-input__inner{
    border-radius: 5px 0 0 5px !important;
    border-right: none;
}
</style>
